package testes;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class CircularShapesTeste extends JPanel implements MouseListener, MouseMotionListener{
    Graphics oGraphicsOff;
    Image oImageScreenOff;
    Dimension oDimensionScreensizeOff;

    public CircularShapesTeste(){
        this.addMouseListener(this);
        //this.addMouseMotionListener(this);
    }

    public void paintComponent(Graphics g)
    {
       System.out.println("PAINTTTT");
       //********************** Creates an empty initial image ***************//
        Dimension oDimension = this.getSize();
        if((oImageScreenOff == null) || (oDimension.width != oDimensionScreensizeOff.width) || (oDimension.height != oDimensionScreensizeOff.height))
        {  oImageScreenOff = createImage(oDimension.width, oDimension.height);
           oDimensionScreensizeOff = oDimension;
           if (oGraphicsOff != null)    oGraphicsOff.dispose();           
           oGraphicsOff = oImageScreenOff.getGraphics();
           oGraphicsOff.setFont(getFont());
        }
        
        oGraphicsOff.setColor(getBackground());
        oGraphicsOff.fillRect(0, 0, oDimension.width, oDimension.height);

       //*********************************************************************//
       //************** ALL is drawed over the empty image********************//
        oGraphicsOff.setColor(Color.black);
        this.setBackground(Color.white);
        
        drawSpiralArchimedes2(oGraphicsOff);

        //******Finally the used image is drawed into the actual screen*******//        
        boolean bTeste = g.drawImage(oImageScreenOff, 0, 0, null);//boolean bTeste= //new Color(255,255,255),
        //System.out.println("cambio->" + bTeste);
    }

   
     public void drawSpiralArchimedes2(Graphics g){
        //********************** r = k * Theta^ a ****************************//
        Graphics2D g2d=(Graphics2D)g;
        double xcenter=this.getWidth()/2, ycenter=this.getHeight()/2;
        double x,y,radio, k=2.5, a=1.5,numRotations=5;
        double ThetaRadianes=0.0;
        GeneralPath aGeneralPath=new GeneralPath();
        aGeneralPath.moveTo(xcenter, ycenter);

        for(double Theta=0;Theta<360*numRotations;Theta+=2){
            ThetaRadianes=Theta*(Math.PI/180);
            radio = k * Math.pow(ThetaRadianes,a);
            x=radio*Math.cos(ThetaRadianes)+xcenter;
            y=radio*Math.sin(ThetaRadianes)+ycenter;

            //g2d.fillOval((int)Math.round(x),(int)Math.round(y),2,2);
            aGeneralPath.lineTo(x, y);
            if((Theta%30) ==0){
            g2d.drawString("aqui", (int)x, (int)y);
            }

        }
       // g2d.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER, 10.0f, null, 0.0f));

         g2d.draw(aGeneralPath);
         /*for(int i=0;i<10; i++){
             g2d.rotate(this.convertCartesianToPolar(10),300,200);//this.convertCartesianToPolar(i*10)
             g2d.drawString("Stringggggggggggggg -> "+i+ " Angle: "+this.convertCartesianToPolar(i*10),300,200);
             System.out.println("String "+i+" : " + this.convertCartesianToPolar(i*10) );
         }*/
         //MyMouseListener oMyMouseListener= new MyMouseListener(g);
//         this.addMouseListener(oMyMouseListener);
//         this.addMouseMotionListener(oMyMouseListener);

    }
     public double convertCartesianToPolar(double angleCartesian){
         return angleCartesian * (Math.PI/180);
     }

    //************************************************************************//
    //class MyMouseListener implements MouseListener, MouseMotionListener{
            Point oPointInitial;
            Point oPointFinal;

            //public MyMouseListener(Graphics pg){
                //   this.g=pg;
            //}


            public void mousePressed(MouseEvent e) {
                addMouseMotionListener(this);
                System.out.println("mousePressed");
                oPointInitial=new Point(e.getX(),e.getY());
                repaint();
                e.consume();
            }

            public void mouseReleased(MouseEvent e) {
                this.removeMouseMotionListener(this);
                System.out.println("mouseReleased");
                //oPointFinal=new Point(e.getX(),e.getY());
                //rotateSpiral(oPointInitial, oPointFinal);
                repaint();
                e.consume();
            }

            public void mouseEntered(MouseEvent e) {
                /*System.out.println("mouseEntered");
                ((Graphics2D)this.oGraphicsOff).rotate(5, 100, 100);
                this.oGraphicsOff.drawString("------------>SHEILA", 100, 100);
                ((Graphics2D)this.oGraphicsOff).rotate(-5, 100, 100);
                this.oGraphicsOff.drawString("REGRESE CON NEGATIVO<--------", 100, 100);
                this.oGraphicsOff.drawImage(oImageScreenOff, 0, 0, null);*/
                
            }
            public void mouseClicked(MouseEvent e) {    }
            public void mouseExited(MouseEvent e) {//contrario de mouseEntered
                System.out.println("mouseExited");
            }
            private Point oPointTemp;
            public void mouseDragged(MouseEvent e) {
                System.out.println("mouseDragged");
                Point oPointTempPre;
                oPointTempPre=(oPointTemp==null)?new Point(e.getX(),e.getY()):oPointTemp;

                oPointTemp=new Point(e.getX(),e.getY());
                rotateSpiral(oPointTempPre, oPointTemp);
                //this.rotateSpiral(oPointInitial, oPointTemp); //no seria oPointInitial
                repaint();
                e.consume();
            }
            public void rotateSpiral(Point oPointI, Point oPointF){
                try{
                double xcenter=CircularShapesTeste.this.getWidth()/2;
                double ycenter=CircularShapesTeste.this.getHeight()/2;
                Point oPointCenter=new Point((int)xcenter,(int)ycenter);

                double ang=this.getAngleRotation(oPointI, oPointF, oPointCenter);
                //double ang=5;
                ((Graphics2D)this.oGraphicsOff).rotate(ang, xcenter, ycenter);
                this.oGraphicsOff.drawString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", (int)xcenter, (int)ycenter);
                this.oGraphicsOff.drawImage(oImageScreenOff, 0, 0, null);
                this.oGraphicsOff.drawString("Centerrrrrrrrrrrrrrrrrrrrrrrrrrrr", (int)xcenter, (int)ycenter);
                
                }catch(Exception e){System.out.println("Error->"+e.getLocalizedMessage());}
            }
            public double getAngleRotation(Point oPointI, Point oPointF, Point oPointCenter){

                double base = Math.sqrt(Math.pow(oPointI.getX()-oPointF.getX(), 2)+Math.pow(oPointI.getY()-oPointF.getY(), 2));
                double hypotenuse = Math.sqrt(Math.pow(oPointI.getX()-oPointCenter.getX(), 2)+Math.pow(oPointI.getY()-oPointCenter.getY(), 2));

                double angle=Math.asin(base/(2*hypotenuse));//OJOOO REVISAR SI EL ARGUMENTO ES MAYOR QUE 1 O SI HAY DIVISION ENTRE 0 ETC.. TRYCATCH
                //if(angle<Math.PI)
                    angle=(+1)*angle;

                //angle=(angle<Math.PI)?-angle:angle;
                return angle;
            }

            public void mouseMoved(MouseEvent e) {

            }
//********************** UTILS ***********************************************//
    public void callTimer(){
         Timer timer2 = new Timer(5000,new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            System.out.println("Timer ejecutandose arriba");
        } });
        timer2.setRepeats(false);
        timer2.start();
    }

    public static void main(String[] args)
    {
        JFrame oJFrame= new JFrame();
        oJFrame.getContentPane().add(new CircularShapesTeste());
        oJFrame.setSize(500, 400);
        oJFrame.show();
    }

}
